package com.xh.xyzp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xh.xyzp.common.constant.UserContext;
import com.xh.xyzp.common.domain.vo.resp.PageResp;
import com.xh.xyzp.common.enums.CommonStatusTypeEnum;
import com.xh.xyzp.domain.entity.Admission;
import com.xh.xyzp.domain.entity.User;
import com.xh.xyzp.domain.vo.AdmissionPageReq;
import com.xh.xyzp.domain.vo.AdmissionResp;
import com.xh.xyzp.domain.vo.AdmissionSubmitReq;
import com.xh.xyzp.exception.ApiException;
import com.xh.xyzp.mapper.AdmissionMapper;
import com.xh.xyzp.service.AdmissionService;
import com.xh.xyzp.service.UserService;
import com.xh.xyzp.util.SnowFlakeIdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class AdmissionServiceImpl extends ServiceImpl<AdmissionMapper, Admission> implements AdmissionService {

    @Autowired
    private AdmissionMapper admissionMapper;

    @Override
    public void submit(AdmissionSubmitReq req) {
        req.checkParams();
        if (Objects.isNull(req.getId())) {
            Admission admission = BeanUtil.copyProperties(req, Admission.class);
            admission.setId(new SnowFlakeIdWorker(1, 0).nextId());
            admission.setUserId(UserContext.getUser().getId());
            save(admission);
        } else {
            Admission admission = admissionMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, admission);
            updateById(admission);
        }
    }

    @Override
    public PageResp<AdmissionResp> getList(AdmissionPageReq req) {
        req.checkParams();
        LambdaQueryWrapper<Admission> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Page<Admission> admissionPage = admissionMapper.selectPage(new Page<>(req.getCurrPage(), req.getPageSize()), lambdaQueryWrapper);
        return PageResp.<AdmissionResp>builder()
                .list(admissionPage.getRecords().stream().map(item -> getInfo(item.getId())).collect(Collectors.toList()))
                .total(admissionPage.getTotal())
                .build();
    }

    @Override
    public AdmissionResp getInfo(Long admissionId) {
        if (Objects.isNull(admissionId)) {
            throw new ApiException("id不能为空");
        }
        Admission admission = lambdaQuery()
                .eq(Admission::getId, admissionId)
                .eq(Admission::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .one();
        AdmissionResp admissionResp = BeanUtil.copyProperties(admission, AdmissionResp.class);
        UserService userService = SpringUtil.getBean(UserService.class);
        User user = userService.getById(admission.getUserId());
        admissionResp.setUsername(user.getUsername());
        admissionResp.setNickname(user.getNickname());
        return admissionResp;
    }
} 